
import java.util.HashMap;
import java.util.Map;

import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;

import java.io.*;
import java.net.*;

import javax.net.ssl.*;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import java.security.*;
import java.security.cert.*;

public class Main {
	
	public static Map<String, Config> mapConfigs = new HashMap<String, Config>();
	//public static Config config;
	public static Menu menu;
	public static Map<String, MenuConfig> mapMenuConfig = new HashMap<String, MenuConfig>();
	
	public static boolean validarExpr(String expr){
		ScriptEngineManager manager = new ScriptEngineManager();
	    ScriptEngine interprete = manager.getEngineByName("js");
	    Boolean retorno=false;
	   
		String s = expr;			
		s=s.replace("||", "");
		s=s.replace("&&", "");
		s=s.replace("(", "");
		s=s.replace(")", "");
		s=s.replace("!", "");
		s=s.replace("  ", " ");
		String[] configs = s.split(" ");    
		
		try {
		for (String string : configs) {
			if(mapConfigs.containsKey(string) && mapConfigs.get(string).getType().getValue() instanceof Boolean){
				interprete.put(string, (boolean) mapConfigs.get(string).getType().getValue());
			}
			else{
				throw new Excepcion("La config de la expresion no existe o no es bool");
			}
		}    
	    retorno= ((boolean) interprete.eval(expr)); 
	    
		}
		catch(ScriptException se) {
	       se.printStackTrace();
	    }
		catch(Excepcion e){
			
		}
		return retorno;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	
		try {
			
			InputStream is= new FileInputStream("e.txt");

			ANTLRInputStream input = new ANTLRInputStream(is);

			// create a lexer that feeds off of input CharStream
			KConfig5Lexer lexer = new KConfig5Lexer(input);

			// create a buffer of tokens pulled from the lexer
			CommonTokenStream tokens = new CommonTokenStream(lexer);

			// create a parser that feeds off the tokens buffer
			KConfig5Parser parser = new KConfig5Parser(tokens);

			ParseTree tree = parser.inicial(); // begin parsing at inicial rule

			ParseTreeWalker walker = new ParseTreeWalker();
			walker.walk(new KConfigToClasses(), tree);//le paso el mapconfigs por param?

			//parserSource(fileCleaned);
			
			
			ClassesToSplot splot = new ClassesToSplot();
			String s= splot.classesToSplot(menu);
			System.out.println(s);
		  } catch(Exception e){
			System.out.println(e);
		  }
		/*if(mapConfigs.isEmpty()){
			System.out.println("mapa vacio");
		}
		else{
			for (Map.Entry<String, Config> entry : mapConfigs.entrySet()){
		    System.out.println(entry.getValue().getSymbol());
		}*/
		
		
		}
		
		
		//System.out.println(config.getSymbol());
		//System.out.println(config.getPrompt().getPrompt());
		//System.out.println(config.getPrompt().getCondicionIf());
		//System.out.println(config.getDepends());
	}
	


